package com.example.cd5160866.modelrecycleview.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * RecyclerView 通用适配器第一版
 * Created by cd5160866 on 16/3/29.
 */
public class ModelRecyclerAdapter<T> extends RecyclerView.Adapter<ModelRecyclerAdapter.ModelViewHolder> {
    protected Context mContext;
    /**
     * 通过注释的方式加入的布局item的layoutId
     */
    protected int mLayoutId;
    /**
     * viewholder的实现类类名
     */
    private Class<? extends ModelViewHolder> viewHolderClass;
    /**
     * 数据 即我们的任何类型的bean
     */
    protected List<T> mData = new ArrayList<>();


    public ModelRecyclerAdapter(Context context, Class<? extends ModelRecyclerAdapter.ModelViewHolder<T>> viewHolderClass) {
        this(context, viewHolderClass, new ArrayList<T>());
    }

    public ModelRecyclerAdapter(Context context, Class<? extends ModelRecyclerAdapter.ModelViewHolder<T>> viewHolderClass, List<T> data) {
        this.mContext = context;
        this.viewHolderClass = viewHolderClass;
        this.mLayoutId = viewHolderClass.getAnnotation(RecyclerItemViewId.class)
                .value();
        this.mData = data;
    }

    @Override
    public ModelViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ModelViewHolder viewHolder = null;

        View convertView = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        try {
            viewHolder = viewHolderClass.getConstructor(View.class).newInstance(convertView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将viewHolder于我们的view绑定起来
        ButterKnife.bind(viewHolder, convertView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ModelViewHolder holder, int position) {
        //这里更新数据
        holder.update(mData.get(position), this, position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void add(T data) {
        mData.add(data);
        notifyDataSetChanged();

    }

    public void add(List<T> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }


    public void replace(int position, T data) {
        mData.remove(position);
        mData.add(position == 0 ? position : position - 1, data);
        notifyDataSetChanged();
    }

    public void addAll(List<T> datas) {
        mData.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public List<T> getItems() {
        return mData;
    }


    public static abstract class ModelViewHolder<T> extends RecyclerView.ViewHolder {

        public ModelViewHolder(View itemView) {
            super(itemView);
        }

        /**
         * 自己去实现数据绑定
         */
        public abstract void update(T item, ModelRecyclerAdapter adapter, int position);
    }
}
